/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.DataObjectEntity;
import entity.PersonEntity;
import entity.SystemUserEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;
import util.Fixtures.Department;

/**
 *
 * @author wei
 */
@Stateless
public class SystemUserEntityFacade extends AbstractFacade<SystemUserEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public SystemUserEntityFacade() {
        super(SystemUserEntity.class);
    }

    public List<SystemUserEntity> getSystemUsersByName(String userName) {
        Query query = em.createQuery(
                "SELECT u FROM SystemUser u WHERE u.name = :inUserName");
        query.setParameter("inUserName", userName);
        List<SystemUserEntity> searchResults = query.getResultList();

        return searchResults;
    }

    public SystemUserEntity getSystemUserByName(String userName) {
        Query query = em.createQuery(
                "SELECT u FROM SystemUser u WHERE u.name = :inUserName");
        query.setParameter("inUserName", userName);
        SystemUserEntity searchResults = (SystemUserEntity) query.getSingleResult();

        return searchResults;
    }

    public PersonEntity getPersonById(Long id) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<DataObjectEntity> dataObjectRoot = cq.from(PersonEntity.class);
        cq.select(dataObjectRoot);
        cq.where(builder.equal(dataObjectRoot.get("id"), id));

        return (PersonEntity) getEntityManager().createQuery(cq).getSingleResult();
    }

    public PersonEntity getPersonByName(String userName) {
        PersonEntity res = null;
        try {
            CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
            javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
            Root<PersonEntity> dataObjectRoot = cq.from(PersonEntity.class);
            cq.select(dataObjectRoot);
            cq.where(builder.and(builder.equal(dataObjectRoot.get("name"), userName), builder.equal(dataObjectRoot.get("archived"), false)));
            
            List<PersonEntity> li = getEntityManager().createQuery(cq).getResultList();
            if(li.size()==0)return null;
            res = (PersonEntity) getEntityManager().createQuery(cq).getSingleResult();
        } catch (NoResultException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }

    public boolean checkUserNameExist(String userName) {
        return getPersonByName(userName)!=null;
    }

    public List<SystemUserEntity> getLockedUser() {
        Query query = (Query) em.createQuery("Select s from SystemUser s WHERE s.locked=true");
        List<SystemUserEntity> list = (List<SystemUserEntity>) query.getResultList();
        return list;
    }

    public List<SystemUserEntity> searchEmployee(String userInput, String searchField, String searchDepartment) {
        if (userInput == null) {
            return new ArrayList<SystemUserEntity>();
        }

        String select = "SELECT C FROM SystemUser C ";
        String queryStr = "";

        String where = " WHERE (C.firstName like " + "'%" + userInput + "%'"
                + " OR C.lastName like " + "'%" + userInput + "%'"
                + " OR C.name like " + "'%" + userInput + "%') ";
        if (searchField != null && searchField.length() > 1) {
            where = " WHERE (C." + "" + searchField + "" + " like " + "'%" + userInput + "%') ";
        }

        if (searchDepartment != null) {
            select = "SELECT C FROM Department D, SystemUser C ";
            String and = " and D=C.department and D.id=" + searchDepartment;
            queryStr = select + where + and;
        } else {
            queryStr = select + where;
        }

        System.out.println(queryStr);
        Query query = em.createQuery(queryStr);
        return query.getResultList();
    }
}
